package net.ezsteam;


import java.util.ArrayList;
import java.util.List;

import net.ezsteam.cons.EzSteam_Urls;
import net.ezsteam.object.EzSteam_Checked_Car;
import net.ezsteam.service.EzSteam_Booking_Service;
import net.ezsteam.service.EzSteam_Booking_Update_Service;
import net.ezsteam.service.EzSteam_GetCars_WithCheck_Service;
import net.ezsteam.service.EzSteam_GetRecalled_Cars;
import net.ezsteam.service.EzSteam_Store_Location;


import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class EzSteam_StoreVisit_Activity extends Activity {

	public static List<EzSteam_Checked_Car> cars = new ArrayList<EzSteam_Checked_Car>();
	
	private ListView myList;
	private String customerid="";
	private String datetime="";
	private String contact_number="";
	private String type="";
	private String appID="";
	private String address="";
	private String locID = "";
	private boolean isUpdate = false;
	private boolean isRecall = false;
	
	int day1=0;
    int month1=0;
    int year1=0;
    
    String spinner_timeslot="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ezsteam_store_visit);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String cusid = extras.getString("cusid");
			datetime = extras.getString("datetime");
			contact_number = extras.getString("contact_number");
			isUpdate = extras.getBoolean("isUpdate");
			isRecall = extras.getBoolean("isRecall");
			type = extras.getString("type");
			appID = extras.getString("appID");
			address = extras.getString("address");
			locID = extras.getString("locID");
			customerid  = cusid;
		}
		
		
		myList = (ListView) findViewById(R.id.list_vehicle_models);

		EzSteam_GetCars_WithCheck_Service eas = new EzSteam_GetCars_WithCheck_Service(EzSteam_StoreVisit_Activity.this,myList,appID);
		eas.execute(EzSteam_Urls.GetCar_Service+"/"+customerid.trim());
		//eas.execute(EzSteam_Urls.GetCar_Service+"/23");
		
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup header = (ViewGroup)inflater.inflate(R.layout.ezsteam_store_visit_header, myList, false);
        ViewGroup footer = (ViewGroup)inflater.inflate(R.layout.ezsteam_store_visit_footer, myList, false);
        myList.addHeaderView(header, null, false);
        myList.addFooterView(footer, null, false);


        Button btn_book = (Button)findViewById(R.id.btn_book);
        final EditText txt_datetime = (EditText)findViewById(R.id.txt_datetime);
        final EditText txt_contact_number = (EditText)findViewById(R.id.txt_contact_number);
        
        final EditText txt_parking = (EditText)findViewById(R.id.txt_parking);
        
        txt_datetime.setText(datetime);
        txt_contact_number.setText(contact_number);
        txt_parking.setText(address);
        
        final TextView txt_Selected = (TextView)findViewById(R.id.txt_selected);
        final TextView txt_LocID= (TextView)findViewById(R.id.txt_locid);
        
        txt_LocID.setText(locID);
        
        final Spinner store_spnr = (Spinner)findViewById(R.id.spn_storelocations);
        
        TextView txt_ser_type = (TextView)findViewById(R.id.txt_ser_type);
        
        
        if(type.equals("0")){
        
        	txt_ser_type.setVisibility(View.VISIBLE);
        	store_spnr.setVisibility(View.VISIBLE);
        	txt_parking.setVisibility(View.GONE);
        	
        	EzSteam_Store_Location sstype = new EzSteam_Store_Location(EzSteam_StoreVisit_Activity.this,store_spnr,txt_Selected,txt_LocID);
        	sstype.execute(EzSteam_Urls.GET_Store_Details);
        
        }else if(type.equals("1")){
        	store_spnr.setVisibility(View.GONE);
        	txt_ser_type.setVisibility(View.GONE);
        	txt_parking.setVisibility(View.VISIBLE);
        }
        
        
        
        txt_datetime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(EzSteam_StoreVisit_Activity.this);

				dialog.setContentView(R.layout.ezsteam_custom_datetimepicker);
				dialog.setTitle("Select Date And Time");

				DatePicker dpk = (DatePicker)dialog.findViewById(R.id.book_datepicker);

				final int day = dpk.getDayOfMonth();
				final int month = dpk.getMonth() + 1;
				final int year = dpk.getYear();
				
				dpk.init(dpk.getYear(), dpk.getMonth(), dpk.getDayOfMonth(), new OnDateChangedListener() {
					
					@Override
					public void onDateChanged(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						
						day1 = dayOfMonth;
						month1 = monthOfYear+1;
						year1 = year;
						
					}
				});
				
				
				final Spinner time_spnr = (Spinner)dialog.findViewById(R.id.spn_time);
				
				spinner_timeslot = time_spnr.getSelectedItem().toString();
				
				time_spnr.setOnItemSelectedListener(new OnItemSelectedListener() {
				    @Override
				    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
				    	spinner_timeslot = time_spnr.getSelectedItem().toString();
				    }

				    @Override
				    public void onNothingSelected(AdapterView<?> parentView) {
				    	
				    }

				});
				
				final String timeslot = time_spnr.getSelectedItem().toString();
				
				Button btndone = (Button)dialog.findViewById(R.id.btn_Done);
				btndone.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Log.i("Date Time Dialog", ""+year1+"-"+((month1<10)?"0"+month1:""+month1)+"-"+((day1<10)?"0"+day1:""+day1)+" "+spinner_timeslot);
						//txt_datetime.setText(""+month+"/"+day+"/"+year+" "+timeslot);
						txt_datetime.setText(""+year1+"-"+((month1<10)?"0"+month1:""+month1)+"-"+((day1<10)?"0"+day1:""+day1)+" "+spinner_timeslot);
						//0000-00-00 00:00:00
						dialog.dismiss();
					}
				});
				
				dialog.show();
			}
		});
        
        btn_book.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isUpdate){
					EzSteam_Booking_Update_Service bknsr = new EzSteam_Booking_Update_Service(EzSteam_StoreVisit_Activity.this,customerid,txt_contact_number.getText().toString(),txt_LocID.getText().toString().trim(),txt_datetime.getText().toString(),appID,txt_parking.getText().toString(),type);
					bknsr.execute(EzSteam_Urls.Book_JustUpdate_Service);
				}else{
					EzSteam_Booking_Service bknsr = new EzSteam_Booking_Service((cars.size()==1)?true:false,0,EzSteam_StoreVisit_Activity.this,customerid,cars,txt_contact_number.getText().toString(),txt_LocID.getText().toString().trim(),txt_datetime.getText().toString(),cars.get(0).getType(),cars.get(0).getCarID(),((type.equals("0"))?txt_Selected.getText().toString():txt_parking.getText().toString()),type);
					bknsr.execute(EzSteam_Urls.Book_Service);
				}
			}
		});
		
	}
	
	
}
